package com.solution.admin.company.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.company.entity.SysPayConfigEntity;
import com.solution.admin.company.service.SysPayConfigService;
import com.solution.admin.product.entity.ProductTagEntity;
import com.solution.common.constants.PublicStatus.SysCode;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 支付方式web控制层
 * @author xkl
 * @date 2017-11-17 10:52:58
 */
@RequestMapping("/sysPayConfig")
@Controller
public class SysPayConfigController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(SysPayConfigController.class);

	@Autowired
	private SysPayConfigService sysPayConfigService;
	
	/**
	 * 跳转支付方式查询页面
	 * @param SysPayConfig 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author xkl
 	 * @date 2017-11-17 10:52:58
	 */
	@RequestMapping("/toListSysPayConfigPage")
	public String toListSysPayConfigPage(SysPayConfigEntity sysPayConfig, HttpServletRequest request) {
		return "company/sysPayConfig_list";
	}

	/**
	 * 分页查询支付方式列表
	 * @param sysPayConfig 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author xkl
 	 * @date 2017-11-17 10:52:58
	 */
	@RequestMapping("/listSysPayConfigPage")
	@ResponseBody
	public JSONGrid listSysPayConfigPage(SysPayConfigEntity sysPayConfig, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("payType", request.getParameter("payType"));
		try {
			PageBean pageBean = sysPayConfigService.listSysPayConfigPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询支付方式失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author xkl
 	 * @date 2017-11-17 10:52:58
	 */
	@RequestMapping("/toAddSysPayConfig")
	public String toAddSysPayConfig(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "company/sysPayConfig_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author xkl
 	 * @date 2017-11-17 10:52:58
	 */
	@RequestMapping("/toUpdateSysPayConfig")
	public String toUpdateSysPayConfig(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			SysPayConfigEntity sysPayConfig = sysPayConfigService.getSysPayConfigById(id);
			request.setAttribute("sysPayConfig", sysPayConfig);
		} catch (Exception e) {
			handleException("跳转编辑支付方式页面错误", logger, e);
		}
		return "company/sysPayConfig_form";
	}


	/**
	 * 新增或保存支付方式
	 * @param request 请求对象
	 * @param sysPayConfigEntity 支付方式实体
	 * @return
	 * @author xkl
 	 * @date 2017-11-17 10:52:58
	 */
	@RequestMapping("/saveOrUpdateSysPayConfig")
	@ResponseBody
	public Map<String, Object> saveSysPayConfig(HttpServletRequest request, SysPayConfigEntity sysPayConfigEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				sysPayConfigEntity.setModifier(getUserCode());
				sysPayConfigService.updateSysPayConfig(sysPayConfigEntity);
			}else{
				sysPayConfigEntity.setCreater(getUserCode());
				sysPayConfigEntity.setStatus(1);
				sysPayConfigService.saveSysPayConfig(sysPayConfigEntity);
			}
			resultMap.put("code", "1");
		} catch (Exception e) {
			handleException("添加支付方式失败", logger, e);
		}
		return resultMap;
	}
	
	/**
	 * 修改支付方式启用停用状态
	 * @param request
	 * @param payId
	 * @param status
	 * @return
	 * @author xkl
	 * @date 2017年11月17日 下午5:19:44
	 */
	@RequestMapping("/modifyStatus")
    @ResponseBody
    public BaseReturn modifyStatus(HttpServletRequest request, Long tagId, Integer status) {
        BaseReturn result = new BaseReturn();
        try {
        	SysPayConfigEntity sysPayConfigEntity = new SysPayConfigEntity();
        	sysPayConfigEntity.setId(tagId);
        	sysPayConfigEntity.setStatus(status);
        	sysPayConfigEntity.setModifier(getUserCode());
            sysPayConfigService.updateSysPayConfig(sysPayConfigEntity);
            result.setCode(SysCode.SUCCESS);
            result.setMsg("支付方式状态修改成功！");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(SysCode.ERROR);
            result.setMsg("支付方式状态修改异常！");
            return result;
        }
    }
	
}
